package com.chookss.home.other;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.KeyEvent;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.view.InitDialog;
import com.chookss.view.listener.ShareDialogListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.lvgroup.hospital.base.BaseAct;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/8.
 */

//社交发布成功页面
public class ScanActivity extends BaseAct { // 实现相关接口
    // 添加一个按钮用来控制闪光灯，同时添加两个按钮表示其他功能，先用Toast表示
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.dbv_custom)
    DecoratedBarcodeView mDBV;
    private CaptureManager captureManager=null;
    @Override
    protected void onPause() {
        super.onPause();
        if (captureManager != null) {
            captureManager.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (captureManager != null) {
            captureManager.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (captureManager != null) {
            captureManager.onDestroy();
        }

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        if (captureManager != null) {
            captureManager.onSaveInstanceState(outState);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        ButterKnife.bind(this);
        initState();
        commonTitleTxt.setText("扫一扫");
    }

    @Override
    public void initData() {
        XXPermissions.with(this)
                .permission(Permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> granted, boolean all) {
                        //重要代码，初始化捕获
                        captureManager = new CaptureManager(ScanActivity.this, mDBV);
                        captureManager.decode();
                        captureManager.onResume();
                    }

                    @Override
                    public void onDenied(List<String> denied, boolean quick) {
                        new InitDialog(ScanActivity.this,
                                "授权提醒", getString(R.string.error_camera_permission), "前往授权", true,
                                new ShareDialogListener() {
                                    @Override
                                    public void onClick() {
                                        XXPermissions.startPermissionActivity(ScanActivity.this, denied);
                                        finish();
                                    }
                                }).show();

                    }
                });
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }
}
